package com.redxiii.spike.pmi.command;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redxiii.spike.api.command.CommandFactory;
import com.redxiii.spike.api.message.ImMessage;
import com.redxiii.spike.api.pmi.entity.Activity;
import com.redxiii.spike.pmi.HandlerSession;
import com.redxiii.spike.pmi.commons.UserSettings;
import com.redxiii.spike.util.command.InternetCommand;
import com.redxiii.spike.util.message.BasicMsgInterpreter;

/**
 * @author Daniel Filgueiras
 * 04/05/2011
 */
@CommandFactory(BasicMsgInterpreter.class)
public class Configure extends InternetCommand {

	private static final Logger logger = LoggerFactory.getLogger(Configure.class);
	
	private String option;
	private String value;
	
	private static String INVALID_OPTION = "Invalid option. Availables are: 'login', 'timer', 'activity'";
	private static String INVALID_TIMER_VALUE = "Invalid value. Availables are: 'auto' and 'manual'";
	private static String ACCEPTED = "Accepted";
	
	@Override
	public boolean proccess(ImMessage message) {
		
		Settings settings = null;
		try {
			settings = Settings.valueOf(option.toLowerCase());
		} catch(IllegalArgumentException e) {
			logger.error("Unable to parse option",e);
			service.reply(message, INVALID_OPTION);
			return true;
		}
		
		boolean accepted;
		
		switch (settings) {
			case timer:
				accepted = handleTimer(message, value);
				break;
				
			case activity:
				accepted = handleActivity(message, value);
				break;
					
			case workedtime:
				accepted = handleWorkedTime(message, value);
				break;
				
			default:
				accepted = false;
		}

		return accepted;
	}
	
	private boolean handleWorkedTime(ImMessage message, String value) {
		
		UserSettings settings = HandlerSession.getUserSettings(remoteId);
		
		if (settings.getWorkingIssue() != 0) {
			if (value.matches("^\\d+(\\.\\d{1,2})?[hm]$")) {
				String subValue = value.substring(0, value.length() - 1);
				float time = Float.parseFloat(subValue);  
				if (value.endsWith("h")) {
					time *= 1000 * 60 * 60;
				}
				if (value.endsWith("m")) {
					time *= 1000 * 60;
				}
				
				settings.setWorkingStartTime(System.currentTimeMillis() - (long)time);
				service.reply(message, ACCEPTED);
				return true;
			}
		}
		return false;
	}

	private boolean handleActivity(ImMessage message, String value) {
		
		UserSettings settings = HandlerSession.getUserSettings(remoteId);
		
		Activity activity = HandlerSession.getDatabase().getActivityByName(value);
		if (activity != null) {
			settings.setActivityId(activity.getId());
			service.reply(message, ACCEPTED);
			return true;
		}
		
		return false;
	}

	private boolean handleTimer(ImMessage message, String value) {
		
		if (value == null || !(value.equals("auto") || value.equals("manual"))) {
			service.reply(message, INVALID_TIMER_VALUE);
			return false;
		}
		
		UserSettings settings = HandlerSession.getUserSettings(remoteId);

		settings.setAutoTimeControl(value.equals("auto"));
		service.reply(message, ACCEPTED);
		
		return true;
	}
}

enum Settings {
	timer,
	activity,
	workedtime,
}
